#Author: 0x9DEFA478


TargetIP=""
TargetUser=""
TargetGDBServerPort=""
TargetSSHServerPort=""
ExecPath=""

options=$(getopt -o "a" --long TargetIP:,TargetUser:,TargetGDBServerPort:,TargetSSHServerPort:,ExecPath: -n $0 -- "$@")
if (( $? != 0 )); then
  exit 1
fi
eval set -- "${options}"
while true
do
  case $1 in
    --TargetIP)
      TargetIP=$2
      shift 2
    ;;
    --TargetUser)
      TargetUser=$2
      IsOutFile="1"
      shift 2
    ;;
    --TargetGDBServerPort)
      TargetGDBServerPort=$2
      shift 2
    ;;
    --TargetSSHServerPort)
      TargetSSHServerPort=$2
      shift 2
    ;;
    --ExecPath)
      ExecPath=$2
      shift 2
    ;;
    --)
      shift
      break
    ;;
    *)
      echo "$0 param decode error $1"
      exit 1
    ;;
  esac
done


if [[ $TargetIP == "" ]]; then
  echo "\e[31mTargetIP no set!\e[0m"
  exit 1
fi
if [[ $TargetUser == "" ]]; then
  echo "\e[31mTargetUser no set!\e[0m"
  exit 1
fi
if [[ $TargetGDBServerPort == "" ]]; then
  echo "\e[31mTargetGDBServerPort no set!\e[0m"
  exit 1
fi
if [[ $ExecPath == "" ]]; then
  echo "\e[31mExecPath no set!\e[0m"
  exit 1
fi

SSHArgs="-o StrictHostKeyChecking=no "

if [[ $TargetSSHServerPort != "" ]]; then
  SSHArgs="-P ${TargetSSHServerPort}"
fi

ExecCMD="gdbserver :${TargetGDBServerPort} ${ExecPath} $*"

ArgumentList="${SSHArgs} ${TargetUser}@${TargetIP}"
ArgumentList="${ArgumentList} ${ExecCMD}"


./shell/NewTerminal.sh ssh $ArgumentList

